#include "contentdisposition.h"
#include "util.h"

CONTENT_DISPOSITION*    
contentdis_new()
{
    CONTENT_DISPOSITION*    cdis;

    cdis = (CONTENT_DISPOSITION*)emalloc(sizeof(CONTENT_DISPOSITION));
    if (cdis == NULL)
        return NULL;
    
    memset(cdis, 0, sizeof(CONTENT_DISPOSITION));

    return cdis;
}



void                    
contentdis_delete(CONTENT_DISPOSITION*  cdis)
{
    if (NULL == cdis)
        return ;

    contentdis_clear(cdis);

    efree(cdis);
}


void                    
contentdis_clear(CONTENT_DISPOSITION*  cdis)
{
    if (NULL == cdis)
        return ;

    SAFE_FREE(cdis->Desc)
    SAFE_FREE(cdis->FileName)
}

int
contentdis_set(void* dis, char* member, char* value)
{
    CONTENT_DISPOSITION* cdis = dis;
    if (NULL == cdis)
        return -1;
    if (NULL == member)
        return -2;
    if (NULL == value)
        return -3;

    switch(member[0])
    {
    case 'd':
        if (cdis->Desc != NULL)
            efree(cdis->Desc);
        cdis->Desc = xstrdup(value);
                
        break;
    case 'f':
        if (cdis->FileName != NULL)
            efree(cdis->FileName);
        cdis->FileName = xstrdup(value);
         
        break;
    default:
        break;
    }
    
    return 0;
}

int                     
contentdis_parse(CONTENT_DISPOSITION*  cdis, char* data)
{
    int     ret;

    if (NULL == cdis)
        return -1;
    if (NULL == data)
        return -2;

    contentdis_clear(cdis);

    ret = fields_get_pairs(data, "desc", cdis, contentdis_set); 
    
    return ret;
}

void
contentdis_print(CONTENT_DISPOSITION*  cdis)
{
    if (NULL == cdis)
        return;

    printf("Content-Disposition\n");
    printf("\tDescription: %s\n", cdis->Desc);
    printf("\tFileName: %s\n", cdis->FileName);
}

int
contentdis_string(CONTENT_DISPOSITION *cdis, char* save, int slen)
{
    if (NULL == cdis)
        return -1;

    if (NULL == cdis->Desc)
        return -2;


}


#ifdef MAIN_TEST

int
main()
{
    int     ret;
    char    *test = "attachment;\n\t filename=\"=?gb2312?B?sNfB7L2hv7W9stf5LnBwdA==?=\"";

    CONTENT_DISPOSITION*  cdis;

    cdis = contentdis_new();

    ret = contentdis_parse(cdis, test);

    printf("ret = %d\n", ret);

    contentdis_print(cdis);

    contentdis_delete(cdis);
    
    return 0;

}




#endif


